RequireVersion ("0.9920060502");


ChoiceList (_idx, "Colors", 1, SKIP_NONE, "Default Colors", "Use the default (20 character) amino-acid palette","Black", "All characters are black");

if (_idx<0)
{
	return 0;
}

_charColorList = {};
if (_idx == 0)
{
	ExecuteCommands ("#include \"ibfs/char_colors.def\";");
}

_idx = "`HYPHY_LIB_DIRECTORY`TemplateBatchFiles`DIRECTORY_SEPARATOR`Utility`DIRECTORY_SEPARATOR`GrabBag.bf";
ExecuteCommands ("#include\"`_idx`\";");
_idx = "`HYPHY_LIB_DIRECTORY`TemplateBatchFiles`DIRECTORY_SEPARATOR`Utility`DIRECTORY_SEPARATOR`PostScript.bf";
ExecuteCommands ("#include\"`_idx`\";");

_seqCount 		= Columns(_DATAPANEL_SELECTED_SEQUENCES_);
_selFilters		= Columns(_DATAPANEL_SELECTED_FILTERS_);

if (_selFilters > 1)
{
	fprintf (stdout, "ERROR: This plug in-needs none or one selected data filter\n");
	return 0;
}

_unitSize 	   = _DATAPANEL_UNIT_SIZE_[0];

if (_unitSize!=1)
{
	fprintf (stdout, "ERROR: This plug can only accept filters with unit size of one\n");
	return 0;
}

if (_selFilters)
{
	_baseName = _DATAPANEL_SELECTED_FILTERS_[0];
}
else
{
	_baseName = _DATAPANEL_DATASET_NAME_;
}

_sequenceMapping = {};
_haveReference = -1;

_exclusions    = _DATAPANEL_EXCLUSIONS_[0];


_font_size  = 8;
_char_space = _font_size;
_page_w	    = 792;
_page_h     = 612;
_atom		= 5;
_seqCaps	= {};

_idx2		= 0;

for (_idx = 0; _idx < _DATAPANEL_DATAFILTER_.species; _idx = _idx+1)
{
	GetString (_capOffset, _DATAPANEL_DATAFILTER_, _idx);
	_idx2 = Max (_idx2, Abs(_capOffset));
	_seqCaps [_idx] = _capOffset;
}


_capOffset	= (_idx2)*_font_size*0.8$1;

DEFAULT_FILE_SAVE_NAME = _baseName + ".ps";
SetDialogPrompt 	  ("Save PostScript graphics to:");

fprintf (PROMPT_FOR_FILE,CLEAR_FILE,KEEP_OPEN,_HYPSPageHeader (_page_w,_page_h,"Character Plot for "+_baseName),
							_HYPSSetFont ("Courier",_font_size),
							"/dmx 6 array currentmatrix def\n",
							"/sshift fs 2 idiv fs add def\n",
							"/setb {0 0 0 setrgbcolor} def\n",
							"/dobox {setrgbcolor newpath 2 copy moveto 4 -1 roll exch 2 copy lineto exch 4 -1 roll 2 copy lineto 4 -1 roll exch lineto pop pop closepath} def\n",
							"/flbx  {dobox fill setb} def\n",
							"/stbx  {dobox stroke setb} def\n",
							"/drln  {setrgbcolor newpath moveto lineto stroke setb} def\n",
							"/drawletter {translate scale newpath 0 0 moveto false charpath fill dmx setmatrix} def\n"
);

outFile = LAST_FILE_PATH;

_page_w		= _page_w-_capOffset;
_selFilters		= Columns(_DATAPANEL_SELECTED_FILTERS_);


GetDataInfo (_charHandles, _DATAPANEL_DATAFILTER_, "CHARACTERS");
_ccount  = Columns (_charHandles);
_unit    = {1,_ccount}["1"];
_char_per_line  = _page_w / _char_space $ _atom * _atom;
if (_char_per_line == 0)
{
	fprintf (stdout, "\nERROR: At least ",_atom," characters must fit in a line; reduce font size in 'Sequence Plot' source\n");
}
else
{
	fprintf (stdout, "\n", _char_per_line, " characters per line\n");
}

_current_x 	  = _capOffset;
_xtra_spacing = _font_size$2 + 1;
_current_y	  = _page_h-(1+_DATAPANEL_DATAFILTER_.species)*_font_size-_xtra_spacing;

GetInformation (_rawStrings, _DATAPANEL_DATAFILTER_);

for (_idx = 0; _idx < _DATAPANEL_DATAFILTER_.sites; _idx = _idx + 1)
{

	/*if (Rows (_cCounter) == 1 && Rows (_cCounter2) == 1)
	{
		if (_cCounter[0][1] != _cCounter2[0][1])
		{
			fprintf (outFile, _current_x, " ", _current_y+_font_size-_font_size$4-1, " ", _current_x + _char_space , " ", _current_y - _dbyLine1[_cl]*_font_size - 2, " 0.2 0.2 0.2 stbx\n");
		}
	}*/
	
	_ref_c = (_rawStrings[0])[_idx];
	
	for (_sidx = _DATAPANEL_DATAFILTER_.species-1; _sidx >= 0; _sidx = _sidx - 1)
	{
		_my_c     = (_rawStrings[_sidx])[_idx];
		if (_sidx && _my_c == _ref_c)
		{
			_my_c = ".";
		}
		if (Abs(_charColorList[_my_c]))
		{
			fprintf (outFile, _charColorList[_my_c], " setrgbcolor\n");
		}
		fprintf (outFile, "(",_my_c,") 1 1 ", _current_x, " ", _current_y+_font_size*(_DATAPANEL_DATAFILTER_.species-1-_sidx), " drawletter\n");
		
		if (Abs(_charColorList[_my_c]))
		{
			fprintf (outFile, "setb\n");
		}
	}
	
	_current_x = _current_x + _char_space;

	if ((_idx + 1)%_char_per_line==0 || _idx == _DATAPANEL_DATAFILTER_.sites-1)
	{
		for (_sidx = _DATAPANEL_DATAFILTER_.species-1; _sidx >= 0; _sidx = _sidx - 1)
		{
			fprintf (outFile, "0 ", _current_y+_font_size*(_DATAPANEL_DATAFILTER_.species-1-_sidx), " moveto (",_seqCaps[_sidx],") show\n");
		}

		_current_y = _current_y + _DATAPANEL_DATAFILTER_.species*_font_size;
		if (_idx == _DATAPANEL_DATAFILTER_.sites-1)
		{
			if (_DATAPANEL_DATAFILTER_.sites % _char_per_line == 0)
			{
				_startx = _char_per_line;
			}
			else
			{
				_startx = _DATAPANEL_DATAFILTER_.sites%_char_per_line$_atom*_atom;
			}
			_idx2 = _DATAPANEL_DATAFILTER_.sites-_DATAPANEL_DATAFILTER_.sites%_char_per_line;
		}
		else
		{
			_idx2 = _idx - _char_per_line + 1;
			_startx = _char_per_line;
		}
		
		/*fprintf (outFile, "0 ", _current_y - _font_size, " moveto (", _seqCaps[0], ") show\n");
		fprintf (outFile, "0 ", _current_y2, " moveto (", _seqCaps[1], ") show\n");*/
				
		fprintf (outFile, _capOffset, " ", _current_y + _font_size * 4$5, " ", _capOffset + _char_space*(1+_char_per_line) , " ", _current_y - _font_size$4, " 0.9 0.9 0.9 flbx\n");
		for (_idx3 = _startx; _idx3 > 0; _idx3 = _idx3 - _atom)
		{
			fprintf (outFile, "(",_idx2+_idx3,") 0.9 0.9 ", _capOffset + (_idx3-1) * _char_space, " ", _current_y, " drawletter\n");
		}

		_current_x = _capOffset; 
		_current_y  = _current_y - (1+2*_DATAPANEL_DATAFILTER_.species)*_font_size - _xtra_spacing;
		_cl = _cl+1;
		if (_current_y < 0)
		{
			_current_y	  = _page_h-(1+_DATAPANEL_DATAFILTER_.species)*_font_size - _xtra_spacing;
			fprintf (outFile, "showpage\n");
		}
	}
}

fprintf (outFile, CLOSE_FILE);

